//
//  LoginController.m
//  iMAL
//
//  Created by Stefano Pigozzi on 2/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "LoginController.h"


@implementation LoginController


@synthesize userLogin;
@synthesize passwordLogin;
@synthesize rememberLogin;

- (void) awakeFromNib
{
	[mal addObserver:self forKeyPath:@"loggedInState" options:(NSKeyValueObservingOptionNew |
															   NSKeyValueObservingOptionOld) context:NULL];
	self.rememberLogin = NO;
	NSUserDefaults * preferences = [NSUserDefaults standardUserDefaults];
	if([preferences boolForKey:@"rememberlogin"]){
		self.rememberLogin = [preferences boolForKey:@"rememberlogin"];
	}
	if(self.rememberLogin == YES){
		if([preferences stringForKey:@"username"]){
			self.userLogin = [preferences stringForKey:@"username"];
		}
		if([preferences stringForKey:@"password"]){
			self.passwordLogin = [preferences stringForKey:@"password"];
		}
	}
	
	if([mal isLoggedIn] == NO){
		[mainWindow setContentSize:[loginView frame].size];
		[mainWindow setResizable:NO];
		[[mainWindow contentView] addSubview:loginView];
	} else {
		[mainWindow setContentSize:[mainView frame].size];
		[[mainWindow contentView] addSubview:mainView];
		[mainView setFrameSize:NSMakeSize(514.0, 422.0)];
	}
}

-(IBAction) login:(id) sender
{
	[circularLoginProgress startAnimation:self];
	[circularLoginProgress setHidden:NO];
	mal.user = self.userLogin;
	mal.password = self.passwordLogin;
	NSUserDefaults * preferences = [NSUserDefaults standardUserDefaults];
	[preferences setValue:self.userLogin forKey:@"username"];
	[preferences setValue:self.passwordLogin forKey:@"password"];
	[preferences setBool:self.rememberLogin forKey:@"rememberlogin"];
	[preferences synchronize];
	[mal login];
}

-(IBAction) logout:(id)sender
{
	[mal logout];
	[[mainWindow contentView] setWantsLayer:YES];
	[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(doLogoutAnimation) userInfo:nil repeats:NO];
}

-(void)doLogoutAnimation
{
	[mainWindow setResizable:NO];
	[loginView setHidden:NO];
	[loginView setFrameSize:NSMakeSize(290.0, 437.0)];
	NSRect newFrame = [self newFrameForNewContentView:loginView];
	[NSAnimationContext beginGrouping];
	if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
	    [[NSAnimationContext currentContext] setDuration:1.0];
	[mainWindow setFrame:newFrame display:YES animate:YES]; // animation effect
	[[mainWindow animator] setContentSize:[loginView frame].size]; // fixes contentview size
	[[[mainWindow contentView] animator] replaceSubview:mainView with:loginView]; // replacing with dissolve animation
	
	[NSAnimationContext endGrouping];
	
	[NSAnimationContext endGrouping];
	[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(finishLogoutAnimation) userInfo:nil repeats:NO]; // we will remove layer backening ASAP!

}

-(void)finishLogoutAnimation
{
	[circularLoginProgress setHidden:YES];
	[[mainWindow contentView] setWantsLayer:NO];
	[mainWindow setMinSize:NSMakeSize(0.0, 0.0)];
}

-(void)doAnimation
{
	[mainWindow setResizable:YES];
	[mainView setFrameSize:NSMakeSize(514.0, 422.0)];
	NSRect newFrame = [self newFrameForNewContentView:mainView];
	[loginView setHidden:YES]; // fixes nstextfield glitch
	[NSAnimationContext beginGrouping];
	
	if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
	    [[NSAnimationContext currentContext] setDuration:1.0];
	[mainWindow setFrame:newFrame display:YES animate:YES]; // animation effect
	[[mainWindow animator] setContentSize:[mainView frame].size]; // fixes contentview size	
	[[[mainWindow contentView] animator] replaceSubview:loginView with:mainView]; // replacing with dissolve animation
	
	[NSAnimationContext endGrouping];
	[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(finishAnimation) userInfo:nil repeats:NO]; // we will remove layer backening ASAP!
	
}

-(void)finishAnimation
{
	[circularLoginProgress setHidden:YES];
	[[mainWindow contentView] setWantsLayer:NO];
	[mainWindow setMinSize:NSMakeSize(514.0, 422.0)];
}

-(NSRect)newFrameForNewContentView:(NSView *)view {
    NSWindow *window = mainWindow;
    NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
    NSRect oldFrameRect = [window frame];
    NSSize newSize = newFrameRect.size;
    NSSize oldSize = oldFrameRect.size;
    
    NSRect frame = [window frame];
    frame.size = newSize;
    frame.origin.y -= (newSize.height - oldSize.height);
	frame.origin.x -= (newSize.width - oldSize.width)/2;
    
    return frame;
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	if([keyPath isEqual:@"loggedInState"]){
		if([[change valueForKey:NSKeyValueChangeNewKey] intValue] == kLoggedIn){
			[[mainWindow contentView] setWantsLayer:YES];
			[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(doAnimation) userInfo:nil repeats:NO];
		} else {
			[circularLoginProgress setHidden:YES];
			[message setTextColor:[NSColor redColor]];
			[message setStringValue:@"Wrong login information. Please retry:"];
		}
	}
}

@end